{% extends "siteSetting/set_base.html" %}
{% block title %}导航栏{% endblock %}
{% block head %}
{{ super() }}
<script src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
{% endblock %}

{% block page_content %}
<h1 class="page-header">导航栏</h1>
<h3>固定导航项</h3>
<h4>
首页 |
买车 |
卖车 |
租赁 |
金融 |
服务
</h4>
<h3>延伸导航项</h3>
<div >
    {% for nav in navs %}
    <input name="name" placeholder="名称" value="{{ nav.name}}">
    <input name="url" placeholder="链接" value="{{ nav.url}}">
    <br>
    {% endfor%}
</div>
<button id="id-button-nav-update" class="btn btn-primary" data-url="/setting/nav/update">保存</button>

{% endblock %}

{% block scripts  %}
{{ super() }}
<script>
    var navUpdateForm = function () {
        var form = [];

        var inputs = $("input[name='name']");
        for (var it = 0; it < inputs.length; it++) {
            var d = {};
            var self = $(inputs[it]);
            d['name'] = self.val();
            d['url'] = self.next().val();
            form.push(d);
        }
        return form
    }
    var send = function (url, method, form, success, error) {
        var request = {
            url: url,
            type: method,
            contentType: 'application/json',
            success: function (r) {
                success(r);
            },
            error: function (err) {
                r = {
                    success: false,
                    data: err
                };
                error(r);
            }
        };
        if (method === 'post') {
            var data = JSON.stringify(form);
            request.data = data;
        }
        $.ajax(request);
    };
    var navUpdate = function () {
        var method = 'post';
        var url = $("#id-button-nav-update").data('url');
        console.log('post', url)
        form = navUpdateForm();
        var success = function (r) {
                alert(r.message);
                location.reload();
            };
        var error = function (err) {
            
        };
        send(url, method, form, success, error);

    }
    var bindActions = function () {
        $('#id-button-nav-update').on('click', function () {
            navUpdate();
        });
    };

    $(document).ready(function () {
        bindActions();
    });

</script>
<script>
    nav = document.getElementById("nav_navigation");
    nav.setAttribute("class", "active");
</script>
{% endblock %}
